package com.zhp.ggweather.adapter;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.zhp.ggweather.R;
import com.zhp.ggweather.View.PinnedHeaderListView;
import com.zhp.ggweather.View.PinnedHeaderListView.PinnedHeaderAdapter;
import com.zhp.ggweather.model.City;

public class CityAdapter extends BaseAdapter implements SectionIndexer,OnScrollListener,PinnedHeaderAdapter{
	
	private Context mContext;
	private List<City> mCities;
	private Map<String, List<City>> mMap;
	private List<String> mSections;
	private List<Integer> mPositions;
	private LayoutInflater inflater;
	
	

	public CityAdapter(Context mContext, List<City> mCities,
			Map<String, List<City>> mMap, List<String> mSections,
			List<Integer> mPositions) {
		super();
		this.mContext = mContext;
		this.mCities = mCities;
		this.mMap = mMap;
		this.mSections = mSections;
		this.mPositions = mPositions;
	}

	@Override
	public int getCount() {
		return mCities.size();
	}

	@Override
	public City getItem(int position) {
		int section=getSectionForPosition(position);
		return mMap.get(mSections.get(section)).get(position-getPositionForSection(section));
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		int section=getSectionForPosition(position);
		if (convertView==null) {
			convertView=inflater.inflate(R.layout.select_city_item, parent,false);
		}
		TextView group=(TextView) convertView.findViewById(R.id.group_title);
		TextView city=(TextView) convertView.findViewById(R.id.colum_title);
		if (getPositionForSection(section)==position) {
			group.setVisibility(View.VISIBLE);
			group.setText(mSections.get(section));
		}else {
			group.setVisibility(View.GONE);
		}
		City item=mMap.get(mSections.get(section)).get(position-getPositionForSection(section));
		city.setText(item.getName());
		return convertView;
	}

	/*
	 * 根据分类列的索引号获得该序列的首个位置
	 */
	@Override
	public int getPositionForSection(int section) {
		if (section<0||section>=mPositions.size()) {
			return -1;
		}
		return mPositions.get(section);
	}

	/*
	 * 通过该项的位置获得所在分类组的索引号
	 */
	@Override
	public int getSectionForPosition(int position) {
		if (position<0||position>=getCount()) {
			return -1;
		}
		int index=Arrays.binarySearch(mPositions.toArray(), position);
		return index>=0?index:-index-2;
	}

	@Override
	public Object[] getSections() {
		return mSections.toArray();
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		//滚动时一直回调，直到停止滚动时才停止回调，单击时回调一次
		//firstVisibleItem：当前能看见的第一个列表项ID(从0开始)
		//visibleItemCount：当前能看见的列表项的个数(小半个也算)
		//totalItemCount：列表项总数
		if (view instanceof PinnedHeaderListView) {
			((PinnedHeaderListView) view).configureHeaderView(firstVisibleItem);
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		
	}

	@Override
	public int getPinnedHeaderState(int position) {
		if (position<0||position>getCount()) {
			return PINNED_HEADER_GONE;
		}
		int section=getSectionForPosition(position);
		int nextSectionPosition=getPositionForSection(section+1);
		if (nextSectionPosition!=-1&&position==nextSectionPosition-1) {
			return PINNED_HEADER_PUSHED_UP;
		}
		return PINNED_HEADER_VISIBLE;
	}

	/*
	 * 将position对应的section字符串设置到head view上去
	 */
	@Override
	public void configurePinnedHeader(View header, int position, int alpha) {
		int section=getSectionForPosition(position);
		String title=(String) getSections()[section];
		((TextView)header.findViewById(R.id.group_title)).setText(title);
	}

}
